<?php


if (!function_exists('getMillisecond')) {
    function getMillisecond() {
        list($s1, $s2) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
    }
}


if (!function_exists('getImgHost')) {
    function getImgHost($uri) {
        return strpos($uri,'http')!==false?$uri:'http://47.115.51.107/'.ltrim($uri,'/');
    }
}

function getcrc16($Source){
        $crc = 0xA1EC;          // initial value
        $polynomial = 0x1021;   // 0001 0000 0010 0001  (0, 5, 12)
        $tmp = "";
        $bytes = array();
        for ($i = 0; $i < strlen($Source) - 1; $i++)
        {
            if ($i % 2 == 0)
            {
                $tmp = substr($Source,$i, 2);
                $bytes[$i / 2] = hexdec($tmp);
            }
        }
        foreach ($bytes as $b)
        {
            for ($i = 0; $i < 8; $i++)
            {
                $bit = (($b >> (7 - $i) & 1) == 1);
                $c15 = (($crc >> 15 & 1) == 1);
                $crc <<= 1;
                if ($c15 ^ $bit) $crc ^= $polynomial;
            }
        }
        $crc &= 0xffff;
        $strDest = $crc;
        return strtoupper(substr("000".dechex($strDest),-4));
}

function getmacno($data){//取机号
        $addrlength=getaddrlength($data);
        $macno=substr(strtohex($data),12,$addrlength*2);
        if($addrlength<5) $macno=hexdec($macno);
        return $macno;
}
    function getprecom($data){//取命令前置固定字节
        $addrlength=getaddrlength($data);
        $command=substr(strtohex($data),0,($addrlength+6)*2);
        return $command;
    }
    function getchildchar($data,$start,$length){//取命令数据中子数组
        $addrlength=getaddrlength($data);
        return substr(strtohex($data),($addrlength+10+$start)*2,$length*2);
    }
    function getaddrlength($data){
        if(ord($data[5])==1||ord($data[5])==4||ord($data[5])==5||ord($data[5])==6||ord($data[5])==7||ord($data[5])==144||ord($data[5])==255)
            return 8;
        if(ord($data[5])==3)
            return 4;
        if(ord($data[5])==254)
            return 5;
        return 2;
    }
    function strtohex($string){//字符串转十六进制
        $hex=bin2hex($string);
        $hex=strtoupper($hex);
        return $hex;
    }
    function hextostr($hex){//十六进制转字符串
        $string=hex2bin($hex);
        return $string;
    }
